//
//  RegisteredController.m
//  soccer
//
//  Created by 上官千城 on 15/9/10.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "RegisteredController.h"
//#import "AppDelegate.h"

@interface RegisteredController ()

@end

@implementation RegisteredController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 注册页面
  
    //从数据库取出token
//    NSMutableArray *arr = [[Account shareHandle]selectAllRegister];
//    Register *reg = [arr lastObject];
////    reg = ;
//    self.token = reg.token;
//    self.sign = reg.sign;
    //背景
    UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    backView.image = [UIImage imageNamed:@"注册详情页_02@2x"];
    [self.view addSubview:backView];
    //返回登陆界面
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setTitle:@"返回登录" forState:UIControlStateNormal];
    back.frame = CGRectMake(10, 40, 100, 20);
    [back addTarget:self action:@selector(backlogin) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];
    //用户名
    self.userName = [[UITextField alloc]initWithFrame:CGRectMake(40, 150, WIDTH - 80, 30)];
    self.userName.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.userName];
    //密码
    self.password1 = [[UITextField alloc]initWithFrame:CGRectMake(self.userName.frame.origin.x, self.userName.frame.origin.y + self.userName.frame.size.height + 30, self.userName.frame.size.width, self.userName.frame.size.height)];
    self.password1.borderStyle = UITextBorderStyleRoundedRect;
    self.password1.secureTextEntry = YES;
    [self.view addSubview:self.password1];
    //确认密码
    self.password2 = [[UITextField alloc]initWithFrame:CGRectMake(self.password1.frame.origin.x, self.password1.frame.origin.y + self.password1.frame.size.height + 30, self.password1.frame.size.width, self.password1.frame.size.height)];
    self.password2.borderStyle = UITextBorderStyleRoundedRect;
    self.password2.secureTextEntry = YES;
    [self.view addSubview:self.password2];
    
    //注册并且登陆按钮
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    [login addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    login.frame = CGRectMake(self.password2.frame.origin.x + 20 , self.password2.frame.origin.y + self.password2.frame.size.height + 30, 120, 30);
    [login setTitle:@"注册登陆" forState:UIControlStateNormal];
    login.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:login];
    //偏好设置按钮
    UIButton *Registered = [UIButton buttonWithType:UIButtonTypeCustom];
    Registered.frame = CGRectMake(login.frame.origin.x + login.frame.size.width +20, login.frame.origin.y, login.frame.size.width, login.frame.size.height);
    [Registered setTitle:@"偏好设置" forState:UIControlStateNormal];
    Registered.backgroundColor = [UIColor orangeColor];
    [Registered addTarget:self action:@selector(registereAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:Registered];
    
    self.regLogic = [[OUserRegLogic alloc] initWithName:@"OUserRegister" muDelegate:self];
    
    self.loginLogic = [[OLoginLogic alloc] initWithName:@"OLogin" muDelegate:self];
    
    self.enterAppLogic = [[OEnterApp alloc] initWithName:@"OEnterApp" muDelegate:self];
    
}

-(void)backlogin
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

//注册登陆
-(void)buttonAction
{
    if (![self.password1.text isEqualToString:self.password2.text]) {
        UIAlertView *samePwd = [[UIAlertView alloc]initWithTitle:@"提示" message:@"两次密码不一致" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [samePwd show];
        //TODO:
    }
   
    //注册用户名密码 赋值
    
    self.regLogic.account = self.userName.text;
    self.regLogic.pwd = self.password1.text;
    self.regLogic.phone = @"1234";
    self.regLogic.protuctId = @"123";
    
    [self.regLogic request];
    
    /*
    
    //注册地址
//    NSString *str1 = [NSString stringWithFormat:@"http://192.168.1.222:9099/OServer/OUserRegister?account=%@&pwd=%@&phone=%@&productId=%@",self.userName.text,self.password1.text,@"123456789",@"600001"];
    
    //登陆地址
//    NSString *str2 = [NSString stringWithFormat:@"http://192.168.1.222:9099/OServer/OLogin?account=%@&pwd=%@",self.userName.text,self.password1.text];
//    

    //进入APP地址
//    NSString *str3 = [NSString stringWithFormat:@"http://192.168.1.222:9099/OServer/OEnterApp?token=%@&sign=%@",self.token,self.sign];
*/
}









//偏好设置
-(void)registereAction
{
    
}

-(void)muRun:(id)logicObj
{
    if ([logicObj isEqual:self.regLogic]) {
        NSLog(@"login start....");
        self.loginLogic.account = self.regLogic.account;
        self.loginLogic.pwd = self.regLogic.pwd;
        [self.loginLogic request];
    }
    
    if ([logicObj isEqual:self.loginLogic]) {
        //TODO:
        self.enterAppLogic.token = self.loginLogic.token;
        self.enterAppLogic.sign = self.loginLogic.sign;
        [self.enterAppLogic request];
        
      
        NSLog(@"enterApp start....");
    }
    
    if ([logicObj isEqual:self.enterAppLogic]) {
        
        Register *reg = [[Register alloc]init];
        reg.userName = self.loginLogic.account;
        reg.password = self.loginLogic.pwd;
        reg.token = self.loginLogic.token;
        reg.sign = self.loginLogic.sign;
        
        [[Account shareHandle]insertRegister:reg];
        
        
        NSLog(@"vvvv");
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.userName resignFirstResponder];
    [self.password1 resignFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
